package top.hekun.study.core.visitor.complex;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.ClassRemapper;
import org.objectweb.asm.commons.Remapper;
import org.objectweb.asm.commons.SimpleRemapper;
import org.objectweb.asm.tree.ClassNode;
import top.hekun.study.core.model.HelloWord;

import java.util.*;

/**
 * 复杂的类修改示例
 * 〈合并接口〉
 *
 * @author: Sakura
 * @date: 2023/5/16
 */
public class MyClassVisitor3 extends ClassVisitor {
    private ClassNode classNode;
    private  String[] newInterfaces;

    public MyClassVisitor3(int apiVersion, ClassVisitor classWriter,ClassNode classNode) {
        super(apiVersion, classWriter);
        this.classNode=classNode;
        // 抽取接口
        List<String> interface_list = classNode.interfaces;
        int size = interface_list.size();
        newInterfaces = new String[size];
        for (int i = 0; i < size; i++) {
            String item = interface_list.get(i);
            newInterfaces[i] = item;
        }
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        Set<String> set = new HashSet<>(); // 注意，这里使用Set是为了避免出现重复接口

        if (interfaces != null) {
            set.addAll(Arrays.asList(interfaces));
        }
        if (newInterfaces != null) {
            set.addAll(Arrays.asList(newInterfaces));
        }
        super.visit(version, access, name, signature, superName, set.toArray(new String[0]));
    }
}
